package com.example.platfrom.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.platfrom.service.ItripHotelService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import io.swagger.annotations.ApiOperation;
import org.example.pojo.ItripHotel;
import org.example.utils.AuthCode;
import org.example.utils.ResultTemplate;
import org.example.vo.recommend.ItripHotel_Recommend;
import org.example.vo.solr.SearchHotel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/hotel")
public class ItripHotelController {
    @Autowired
    private ItripHotelService hotelService;

    @ApiOperation(value = "查询酒店分页", httpMethod = "GET")
    @GetMapping(value = "/queryAll")
    public Object queryAll(Integer pageNum,String city){
        IPage page = hotelService.queryAll(pageNum, city);
        if (page!=null)
            return new ResultTemplate(AuthCode.AUTH_SUCCESS,page);
        else
            return new ResultTemplate(AuthCode.AUTH_ERROR_SYSTEM);
    }

    @ApiOperation(value = "查询酒店推荐", httpMethod = "GET")
    @GetMapping(value = "/hotelRecommend")
    public Object queryRecommend(String city){
        System.out.println(city);
        List<ItripHotel_Recommend> recommends = hotelService.queryHotelRecommend(city);
        for (ItripHotel_Recommend recommend : recommends) {
            System.out.println(recommend);
        }
        return new ResultTemplate(AuthCode.AUTH_SUCCESS,recommends);
    }

    @ApiOperation(value = "查询酒店根据solr检索",httpMethod = "POST")
    @PostMapping(value = "/hotelSearchBySolr")
    @HystrixCommand(fallbackMethod = "querySearchGetByFallBack")
    public Object queryHotelSearchSolr(String hotelName,String cityName) throws Exception{
        List<SearchHotel> searchHotels = hotelService.queryBySolrCondition(hotelName, cityName);
        if(searchHotels.size() <= 0 || searchHotels == null){
            throw new Exception("solr检索失败");
        }
        return new ResultTemplate(AuthCode.AUTH_SUCCESS,searchHotels);
    }
    //熔断降级方法（查询mysql数据）
    public Object querySearchGetByFallBack(String hotelName,String cityName){
        System.out.println("------------进入降级方法---------------");
        List<SearchHotel> searchHotels = hotelService.queryFallBack(hotelName, cityName);
        return new ResultTemplate(AuthCode.AUTH_SUCCESS,searchHotels);
    }

    @ApiOperation(value = "查询酒店详情信息",httpMethod = "GET")
    @GetMapping(value = "/hotelDetail")
    public Object queryHotelDetail(Long id){
        Map<String, Object> map = hotelService.queryHotelDetails(id);
        if (map.size()>0 && map!=null)
            return new ResultTemplate<>(AuthCode.AUTH_SUCCESS,map);
        else
            return new ResultTemplate<>(AuthCode.AUTH_ERROR_SYSTEM);
    }

}
